#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int T;
    cin >> T;
    while (T--) {
        int nt, k;
        cin >> nt >> k;
        vector<int> b(nt);
        for (int i = 0; i < nt; i++) {
            string ai;
            int bi;
            cin >> ai >> bi;
            b[i] = bi;
        }

        int total = 0;
        for (int bi : b) {
            total += bi / 3;
        }
        if (total < k) {
            cout << -1 << endl;
            continue;
        }

        int left = 3 * k;
        int right = 0;
        for (int bi : b) {
            right += bi;
        }
        int answer = right;

        while (left <= right) {
            int mid = (left + right) / 2;
            int max_possible = 0;
            for (int bi : b) {
                int x = min(bi, mid);
                max_possible += x / 3;
            }
            if (max_possible >= k) {
                answer = mid;
                right = mid - 1;
            }
            else {
                left = mid + 1;
            }
        }
        cout << answer << endl;
    }
    return 0;
}